<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Area;
use app\models\Cancel;
use app\models\Config;
use app\models\Goods;
use app\models\Income;
use app\models\Manager;
use app\models\Order;
use app\models\OrderCancel;
use app\models\Play;
use app\models\Shop;
use app\models\Tag;
use app\models\User;
use yii\db\Query;



class OrderLogic
{
    public function createCancelNo(){
        return mt_rand(10000000,99999999);
        /*$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        return $yCode[intval(date('Y')) - 2018] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));*/
    }

    public function createOrderNo(){
        return date('Ymd').mt_rand(100000,999999);
        /*$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        return $yCode[intval(date('Y')) - 2018] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));*/
    }

    public function saveOrder($order_no,$cancel_no,$type,$gp_id,$price,$current_num,$expire_time,$user_id,$shop_id=0,$ext_price=0){
        $order = new Order();
        $order->order_no = $order_no;
        $order->cancel_no = $cancel_no;
        $order->type = $type;
        $order->status = 0;
        $order->gp_id = $gp_id;
        $order->price = $price;
        $order->current_num = $current_num;
        $order->order_time = date('Y-m-d H:i:s');
        $order->expire_time = $expire_time;
        $order->user_id = $user_id;
        $order->shop_id = $shop_id;
        if($ext_price)
            $order->ext_price = $ext_price;
        return $order->save();
    }

    public function getOrder($order_no){
        return Order::find()
            ->select('o.*,u.phone')
            ->from('{{%order}} o')
            ->leftJoin('{{%user}} u','u.user_id=o.user_id')
            ->where("o.order_no='$order_no'")
            ->asArray()
            ->one();
    }

    public function getOrderByCancelNo($cancel_no){
        return Order::find()
            ->select('o.*,u.phone')
            ->from('{{%order}} o')
            ->leftJoin('{{%user}} u','u.user_id=o.user_id')
            ->where("o.cancel_no='$cancel_no'")
            ->asArray()
            ->one();
    }

    public function cancelScanCode($manager_code,$user_id,$cancel_num){

        $user = User::findOne(['user_id'=>$user_id]);
        if($user->current_num>0){
            $db=\Yii::$app->db;
            $transaction = $db->beginTransaction();
            $manager = Manager::findOne(['manager_code'=>$manager_code]);
            $cancel = new Cancel();
            $cancel->manager_id = $manager->manager_id;
            $cancel->shop_id = $manager->shop_id;
            $shop = Shop::findOne($manager->shop_id);
            $owner_user_id = $shop->owner_user_id;
            $income1 = new Income();
            $income2 = new Income();
            $commissionConfig = Config::find()->where(['config_name'=>'commission','plan_id'=>$shop->plan_id])->asArray()->all();
            $commissionConfigTemp = [];
            foreach($commissionConfig AS $k=>$v){
                $config_ext = json_decode($v['config_ext'],true);
                $commissionConfigTemp[(string)$config_ext['role']][(string)$config_ext['type']] = $v['config_value'];
            }
            $time = date('Y-m-d H:i:s');
            $ordertemp = Order::find()
                ->from('{{%order}} o')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
                ->where("o.user_id=$user_id AND o.type=0 AND o.current_num>0 AND p.type=1")
                ->orderBy('o.order_id')
                ->asArray()
                ->all();
            $need_cancel_num = $cancel_num;
            $result2 = true;
            $cancel->type = 1;
            $cancel->num = $cancel_num;
            $cancel->cancel_time = $time;
            $result3 = $cancel->save();
            $result6 = true;
            foreach($ordertemp AS $kk=>$vv){
                $order3 = Order::findOne(['order_id'=>$vv['order_id']]);
                $ticheng = $order3->ext_price;
                if($need_cancel_num==0)
                    break;
                if($order3->current_num<$need_cancel_num){
                    $need_cancel_num = $need_cancel_num-$order3->current_num;
                    $order3->current_num = 0;
                    $order3->status=4;
                    //$order3->cancel_id = $cancel->cancel_id;
                    $result2 &= $order3->save();

                    $ordercancel = new OrderCancel();
                    $ordercancel->order_id = $order3->order_id;
                    $ordercancel->cancel_id = $cancel->cancel_id;
                    $result6 &= $ordercancel->save();

                }else{
                    $order3->current_num -= $need_cancel_num;
                    $need_cancel_num = 0;
                    //$order3->cancel_id = $cancel->cancel_id;
                    if($order3->current_num==0)
                        $order3->status=4;
                    else
                        $order3->status=3;
                    $result2 &= $order3->save();

                    $ordercancel = new OrderCancel();
                    $ordercancel->order_id = $order3->order_id;
                    $ordercancel->cancel_id = $cancel->cancel_id;
                    $ordercancel->save();
                    break;
                }
            }
            //$cancel->order_id = $vv['order_id'];
            $user->current_num -= $cancel_num;
            $result1 = $user->save();
            $income1->user_id = $user_id;
            $income1->cancel_id = $cancel->cancel_id;
            $income1->type = 2;
            $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)2][(string)1]/100;
            $income1->commission = $commission;
            $income1->income_time = $time;
            $result4 = $income1->save();
            $income2->user_id = $owner_user_id;
            $income2->cancel_id = $cancel->cancel_id;
            $income2->type = 1;
            $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)1][(string)1]/100;
            $income2->commission = $commission;
            $income2->income_time = $time;
            $result5 = $income2->save();
            if($result1 && $result2 && $result3 && $result4 && $result5 && $result6){
                $transaction->commit();
                return true;
            }else{
                $transaction->rollBack();
                return false;
            }
        }else{
            return false;
        }
    }




/*`status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '订单状态：0未支付,1已付款，2,已提货，3使用中，4已使用或已回收',
@apiParam {int} type 类型：默认0为核销体验券，1为核销体验卡，2为提货商品，3为回收商品'*/
    public function cancelOrder($cancel_no,$user_id,$type=-1,$cancel_num=0,$yoyo_type=0){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $order = Order::findOne(['cancel_no'=>$cancel_no]);
        if(!$order)
            return false;
        if($order->status == 4){
            return false;
        }
        $manager = Manager::findOne(['user_id'=>$user_id]);
        $cancel = new Cancel();
        //$cancel->order_id = $order->order_id;
        $cancel->manager_id = $manager->manager_id;
        $cancel->shop_id = $manager->shop_id;
        $shop = Shop::findOne(['shop_id'=>$manager->shop_id]);
        $owner_user_id = $shop->owner_user_id;
        $income1 = new Income();
        $income2 = new Income();
        $commissionConfig = Config::find()->where(['config_name'=>'commission','plan_id'=>$shop->plan_id])->asArray()->all();
        $commissionConfigTemp = [];
        $time = date('Y-m-d H:i:s');
        foreach($commissionConfig AS $k=>$v){
            $config_ext = json_decode($v['config_ext'],true);
            $commissionConfigTemp[(string)$config_ext['role']][(string)$config_ext['type']] = $v['config_value'];
        }
        if($type==-1){
            if($order->type==0){
                $play = Play::findOne(['play_id'=>$order->gp_id]);
                if($play->type==0){
                    $type = 0;
                }else{
                    $type = 1;
                }
            }elseif($order->type==1){
                if($order->status==1){
                    $type = 2;
                }else{
                    $type = 3;
                }
            }elseif($yoyo_type==1){
                //$type = 4;
                $type = 1;
            }
        }
        $cancel->type = $type;
        $cancel->cancel_time = $time;
        if($cancel_num){
            $cancel->num = $cancel_num;
        }
        $result1 = $result4 = true;
        $result2 = $result3 = true;
        $result5 = true;
        $user = User::findOne(['user_id'=>$order->user_id]);
        if($order->type==0){
            $play = Play::findOne(['play_id'=>$order->gp_id]);
            if($play->type && /*($order->status==1||$order->status==3) &&*/ ($type==1 || $type==4)){

                /*if($yoyo_type){
                    $usertemp = $order->user_id;
                    if($user->current_num>0){
                        $ordertemp = Order::find()
                            ->from('{{%order}} o')
                            ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
                            ->where("o.user_id=$usertemp AND o.type=0 AND o.current_num>0 AND p.type=1 AND o.status!=0")
                            ->orderBy('o.order_id')
                            ->asArray()
                            ->all();
                        $need_cancel_num = $cancel_num;
                        $result2 = true;
                        $result3 = $cancel->save();

                        foreach($ordertemp AS $kk=>$vv){
                            $order3 = Order::findOne(['order_id'=>$vv['order_id']]);
                            $ticheng = $order3->ext_price;
                            if($need_cancel_num==0)
                                break;
                            if($order3->current_num<$need_cancel_num){
                                $need_cancel_num = $need_cancel_num-$order3->current_num;
                                $order3->current_num = 0;
                                $order3->status=4;
                                $result2 &= $order3->save();
                                $ordercancel = new OrderCancel();
                                $ordercancel->order_id = $vv['order_id'];
                                $ordercancel->cancel_id = $cancel->cancel_id;
                                //$order3->cancel_id = $cancel->cancel_id;
                                $result5 &= $ordercancel->save();
                            }else{
                                $order3->current_num -= $need_cancel_num;
                                $need_cancel_num = 0;
                                if($order3->current_num==0)
                                    $order3->status=4;
                                else
                                    $order3->status=3;
                                $result2 &= $order3->save();
                                $ordercancel = new OrderCancel();
                                $ordercancel->order_id = $vv['order_id'];
                                $ordercancel->cancel_id = $cancel->cancel_id;
                                //$order3->cancel_id = $cancel->cancel_id;
                                $result5 &= $ordercancel->save();
                                break;
                            }
                        }

                        $user->current_num -= $cancel_num;
                        $result1 = $user->save();

                        $income1->user_id = $user_id;
                        $income1->cancel_id = $cancel->cancel_id;
                        $income1->type = 2;
                        $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)2][(string)$type]/100;
                        $income1->commission = $commission;
                        $income1->income_time = $time;
                        $income1->save();
                        $income2->user_id = $owner_user_id;
                        $income2->cancel_id = $cancel->cancel_id;
                        $income2->type = 1;
                        $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)1][(string)$type]/100;
                        $income2->commission = $commission;
                        $income2->income_time = $time;
                        $income2->save();
                    }else{
                        return false;
                    }
                }else{*/
                    $usertemp = $order->user_id;
                    if($user->current_num>0){
                        $ordertemp = Order::find()
                            ->from('{{%order}} o')
                            ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
                            ->where("o.user_id=$usertemp AND o.type=0 AND o.current_num>0 AND p.type=1 AND o.status!=0")
                            ->orderBy('o.order_id')
                            ->asArray()
                            ->all();
                        $need_cancel_num = $cancel_num;
                        $result2 = true;
                        $result3 = $cancel->save();

                        foreach($ordertemp AS $kk=>$vv){
                            $order3 = Order::findOne(['order_id'=>$vv['order_id']]);
                            $ticheng = $order3->ext_price;
                            if($need_cancel_num==0)
                                break;
                            if($order3->current_num<$need_cancel_num){
                                $need_cancel_num = $need_cancel_num-$order3->current_num;
                                $order3->current_num = 0;
                                $order3->status=4;
                                $result2 &= $order3->save();
                                $ordercancel = new OrderCancel();
                                $ordercancel->order_id = $vv['order_id'];
                                $ordercancel->cancel_id = $cancel->cancel_id;
                                //$order3->cancel_id = $cancel->cancel_id;
                                $result5 &= $ordercancel->save();
                            }else{
                                $order3->current_num -= $need_cancel_num;
                                $need_cancel_num = 0;
                                if($order3->current_num==0)
                                    $order3->status=4;
                                else
                                    $order3->status=3;
                                $result2 &= $order3->save();
                                $ordercancel = new OrderCancel();
                                $ordercancel->order_id = $vv['order_id'];
                                $ordercancel->cancel_id = $cancel->cancel_id;
                                //$order3->cancel_id = $cancel->cancel_id;
                                $result5 &= $ordercancel->save();
                                break;
                            }
                        }

                        $user->current_num -= $cancel_num;
                        $result1 = $user->save();

                        $income1->user_id = $user_id;
                        $income1->cancel_id = $cancel->cancel_id;
                        $income1->type = 2;
                        $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)2][(string)$type]/100;
                        $income1->commission = $commission;
                        $income1->income_time = $time;
                        $income1->save();
                        $income2->user_id = $owner_user_id;
                        $income2->cancel_id = $cancel->cancel_id;
                        $income2->type = 1;
                        $commission = $cancel_num*($ticheng)*$commissionConfigTemp[(string)1][(string)$type]/100;
                        $income2->commission = $commission;
                        $income2->income_time = $time;
                        $income2->save();
                    }else{
                        return false;
                    }

                //}




                /*if($order->current_num>0){
                    $user->current_num--;
                    $result1 = $user->save();
                    $order->current_num--;
                    if($order->current_num==0)
                        $order->status=4;
                    else
                        $order->status=3;
                    $result2 = $order->save();
                    $result3 = $cancel->save();
                    $income1->user_id = $user_id;
                    $income1->cancel_id = $cancel->cancel_id;
                    $income1->type = 2;
                    $commission = $order->ext_price*$commissionConfigTemp[(string)2][(string)$type]/100;
                    $income1->commission = $commission;
                    $income1->income_time = $time;
                    $income1->save();
                    $income2->user_id = $owner_user_id;
                    $income2->cancel_id = $cancel->cancel_id;
                    $income2->type = 1;
                    $commission = $order->ext_price*$commissionConfigTemp[(string)1][(string)$type]/100;
                    $income2->commission = $commission;
                    $income2->income_time = $time;
                    $income2->save();
                }else{
                    $usertemp = $order->user_id;
                    $ordertemp = Order::find()
                        ->from('{{%order}} o')
                        ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
                        ->where("o.user_id=$usertemp AND o.type=0 AND o.current_num>0 AND p.type=1")
                        ->orderBy('o.order_id')
                        ->asArray()
                        ->one();
                    if($ordertemp){
                        $order3 = Order::findOne(['order_id'=>$ordertemp['order_id']]);
                        $user->current_num--;
                        $result1 = $user->save();
                        $order3->current_num--;
                        if($order3->current_num==0)
                            $order3->status=4;
                        else
                            $order3->status=3;
                        $result2 = $order3->save();
                        $cancel->order_id = $ordertemp['order_id'];
                        $result3 = $cancel->save();
                        $income1->user_id = $user_id;
                        $income1->cancel_id = $cancel->cancel_id;
                        $income1->type = 2;
                        $commission = $order3->ext_price*$commissionConfigTemp[(string)2][(string)$type]/100;
                        $income1->commission = $commission;
                        $income1->income_time = $time;
                        $income1->save();
                        $income2->user_id = $owner_user_id;
                        $income2->cancel_id = $cancel->cancel_id;
                        $income2->type = 1;
                        $commission = $order3->ext_price*$commissionConfigTemp[(string)1][(string)$type]/100;
                        $income2->commission = $commission;
                        $income2->income_time = $time;
                        $income2->save();
                    }else{
                        return false;
                    }
                }*/
            }elseif($play->type==0 && $order->status==1 && $type==0){
                $result3 = $cancel->save();
                //$order->cancel_id = $cancel->cancel_id;
                $order->status=4;
                $result2 = $order->save();

                $ordercancel = new OrderCancel();
                $ordercancel->order_id = $order->order_id;
                $ordercancel->cancel_id = $cancel->cancel_id;
                $result5 &= $ordercancel->save();

                $income1->user_id = $user_id;
                $income1->cancel_id = $cancel->cancel_id;
                $income1->type = 2;
                $commission = $order->price*$commissionConfigTemp[(string)2][(string)$type]/100;
                $income1->commission = $commission;
                $income1->income_time = $time;
                $income1->save();
                $income2->user_id = $owner_user_id;
                $income2->cancel_id = $cancel->cancel_id;
                $income2->type = 1;
                $commission = $order->price*$commissionConfigTemp[(string)1][(string)$type]/100;
                $income2->commission = $commission;
                $income2->income_time = $time;
                $income2->save();
            }
        }elseif($order->type==1){
            $goods = Goods::findOne(['goods_id'=>$order->gp_id]);
            $play = Play::findOne(['play_id'=>$goods->play_id]);
            if($order->status==1 && $type==2){
                $result3 = $cancel->save();
                $order->status=2;
                //$order->cancel_id = $cancel->cancel_id;
                $result2 = $order->save();

                $ordercancel = new OrderCancel();
                $ordercancel->order_id = $order->order_id;
                $ordercancel->cancel_id = $cancel->cancel_id;
                $result5 &= $ordercancel->save();

                $income1->user_id = $user_id;
                $income1->cancel_id = $cancel->cancel_id;
                $income1->type = 2;
                $commission = $order->price*$commissionConfigTemp[(string)2][(string)$type]/100;
                $income1->commission = $commission;
                $income1->income_time = $time;
                $income1->save();
                $income2->user_id = $owner_user_id;
                $income2->cancel_id = $cancel->cancel_id;
                $income2->type = 1;
                $commission = $order->price*$commissionConfigTemp[(string)1][(string)$type]/100;
                $income2->commission = $commission;
                $income2->income_time = $time;
                $income2->save();
            }elseif($order->status==2 && $type==3){
                $order->status=4;
                //回收下单
                $order1 = new Order();
                $order_no = $this->createOrderNo();
                $order2 = Order::findOne(['order_no'=>$order_no]);
                while($order2){
                    $order_no = $this->createOrderNo();
                    $order2 = Order::findOne(['order_no'=>$order_no]);
                }
                $cancel_no1 = $this->createCancelNo();
                $order3 = Order::findOne(['cancel_no'=>$cancel_no1]);
                while($order3){
                    $cancel_no1 = $this->createCancelNo();
                    $order3 = Order::findOne(['cancel_no'=>$cancel_no1]);
                }
                $order1->order_no = $order_no;
                $order1->cancel_no = $cancel_no1;
                $order1->type = 0;
                $order1->status = 1;
                $order1->gp_id = $goods->play_id;
                $order1->price = $play->price;
                $order1->current_num = $play->num;
                $order1->order_time = $time;
                $order1->expire_time = date('Y-m-d', strtotime("+".$play['expire_time']." day"));
                $order1->user_id = $order->user_id;
                $order1->pay_time = $time;
                $order1->ext_goods_id = $order->gp_id;
                $user->current_num += $play->num;
                $result1 = $user->save();
                $result4 = $order1->save();
                //回收下单
                $result3 = $cancel->save();
                //$order->cancel_id = $cancel->cancel_id;
                $result2 = $order->save();

                $ordercancel = new OrderCancel();
                $ordercancel->order_id = $order->order_id;
                $ordercancel->cancel_id = $cancel->cancel_id;
                $result5 &= $ordercancel->save();

                $income1->user_id = $user_id;
                $income1->cancel_id = $cancel->cancel_id;
                $income1->type = 2;
                $commission = $order->price*$commissionConfigTemp[(string)2][(string)$type]/100;
                $income1->commission = $commission;
                $income1->income_time = $time;
                $income1->save();
                $income2->user_id = $owner_user_id;
                $income2->cancel_id = $cancel->cancel_id;
                $income2->type = 1;
                $commission = $order->price*$commissionConfigTemp[(string)1][(string)$type]/100;
                $income2->commission = $commission;
                $income2->income_time = $time;
                $income2->save();
            }
        }elseif($order->type==2){
            $result3 = $cancel->save();
            //$order->cancel_id = $cancel->cancel_id;
            $order->status=4;
            $result2 = $order->save();

            $ordercancel = new OrderCancel();
            $ordercancel->order_id = $order->order_id;
            $ordercancel->cancel_id = $cancel->cancel_id;
            $result5 &= $ordercancel->save();

            $income1->user_id = $user_id;
            $income1->cancel_id = $cancel->cancel_id;
            $income1->type = 2;
            $commission = $order->price*$commissionConfigTemp[(string)2][(string)$type]/100;
            $income1->commission = $commission;
            $income1->income_time = $time;
            $income1->save();
            $income2->user_id = $owner_user_id;
            $income2->cancel_id = $cancel->cancel_id;
            $income2->type = 1;
            $commission = $order->price*$commissionConfigTemp[(string)1][(string)$type]/100;
            $income2->commission = $commission;
            $income2->income_time = $time;
            $income2->save();
        }
        if($result1 && $result2 && $result3 && $result4 && $result5){
            $transaction->commit();
            return $order->order_no;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function getLatestPlayCard($user_id){
        $time = date('Y-m-d');
        return Order::find()
            ->from('{{%order}} o')
            ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
            ->where("o.user_id=$user_id AND o.type=0 AND (o.status=1 OR o.status=3) AND o.current_num>0 AND p.type=1 AND o.expire_time>='$time'")
            ->orderBy('o.order_id')
            ->asArray()
            ->one();
    }

    public function getOrderList($user_id,$type=0)
    {
        if ($type) {
            return Order::find()
                ->select('o.*,p.*,o.expire_time as expire_time,o.price as price,o.status as status,o.type as type')
                ->from('{{%order}} o')
                ->leftJoin('{{%play}} p', 'p.play_id=o.gp_id AND o.type=0')
                ->where("o.user_id=$user_id AND p.type=1 AND o.status!=0")
                ->orderBy('o.order_id desc')
                ->asArray()
                ->all();

        } else {
            return Order::find()
                ->select('o.*,p.*,g.*,d.*,o.type as type,o.expire_time as expire_time,o.price as price,o.status as status,o.type as type,c.cancel_time')
                ->from('{{%order}} o')
                ->leftJoin('{{%play}} p', 'p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g', 'g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%device}} d', 'd.device_id=o.gp_id  AND o.type=2')
                ->leftJoin('{{%order_cancel}} oc', 'oc.order_id=o.order_id')
                ->leftJoin('{{%cancel}} c', 'c.cancel_id=oc.cancel_id AND (c.type=0 OR c.type=3)')
                ->where("o.user_id=$user_id AND (p.type!=1 OR o.type=1) AND o.status!=0")
                ->orderBy('o.order_id desc')
                ->asArray()
                ->all();
        }
    }


        public function getOrderListByManager($user_id, $type=-1){
            if($type==-1){
                /*return Order::find()
                    ->select('group_concat(o.order_no) as order_no,o.gp_id,u.phone,o.type AS order_type,o.type as type,c.num,c.cancel_time')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                    ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.order_id=o.order_id')
                    ->leftJoin('{{%cancel}} c','c.cancel_id=oc.cancel_id')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id AND o.cancel_id is not null")
                    ->groupBy('oc.cancel_id,o.type,o.gp_id,u.phone')
                    ->orderBy('c.cancel_time desc')
                    ->asArray()
                    ->all();*/
                return Cancel::find()
                    ->select('group_concat(o.order_no) as order_no,o.gp_id,u.phone,group_concat(o.expire_time) AS expire_date,o.type AS order_type,group_concat(o.price) as price,o.type as type,c.cancel_time,c.num as num')
                    ->from('{{%cancel}} c')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                    ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                    ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                    ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                    ->leftJoin('{{%device}} d','d.device_id=o.gp_id  AND o.type=2')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id")
                    ->groupBy('c.cancel_id,o.gp_id,u.phone,o.type')
                    ->orderBy('c.cancel_id desc')
                    ->asArray()
                    ->all();
            }elseif($type==0){
                /*return Order::find()
                    ->select('group_concat(o.order_no) as order_no,o.gp_id,u.phone,o.type AS order_type,o.type as type,c.num,c.cancel_time')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.order_id=o.order_id')
                    ->leftJoin('{{%cancel}} c','c.cancel_id=oc.cancel_id')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id AND o.type=$type AND o.cancel_id is not null")
                    ->groupBy('o.cancel_id,o.type,o.gp_id,u.phone')
                    ->orderBy('c.cancel_time desc')
                    ->asArray()
                    ->all();*/
                return Cancel::find()
                    ->select('group_concat(o.order_no) as order_no,o.gp_id,u.phone,group_concat(o.expire_time) AS expire_date,o.type AS order_type,group_concat(o.price) as price,o.type as type,c.cancel_time,c.num as num')
                    ->from('{{%cancel}} c')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                    ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                    ->leftJoin('{{%play}} p','p.play_id=o.gp_id  AND o.type=0')
                    ->leftJoin('{{%device}} d','d.device_id=o.gp_id  AND o.type=2')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id AND (o.type=0 OR o.type=2)")
                    ->groupBy('c.cancel_id,o.gp_id,u.phone,o.type')
                    ->orderBy('c.cancel_id desc')
                    ->asArray()
                    ->all();
            }else{
                /*return Order::find()
                    ->select('order_no,o.gp_id,u.phone,o.type AS order_type,o.type as type,c.num,c.cancel_time')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.order_id=o.order_id')
                    ->leftJoin('{{%cancel}} c','c.cancel_id=oc.cancel_id')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id AND o.type=$type AND o.cancel_id is not null")
                    ->orderBy('c.cancel_time desc')
                    ->asArray()
                    ->all();*/
                return Cancel::find()
                    ->select('o.*,g.*,u.phone,o.expire_time AS expire_date,o.type AS order_type,o.price as price,o.type as type,c.cancel_time,c.num as num')
                    ->from('{{%cancel}} c')
                    ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                    ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                    ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                    ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->where("m.user_id=$user_id AND o.type=$type")
                    ->orderBy('c.cancel_id desc')
                    ->asArray()
                    ->all();
            }
        }



    public function getCancelList($user_id){
        return Cancel::find()
            ->select('c.*,c.type AS is_card,o.*,s.*')
            ->from('{{%cancel}} c')
            ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
            ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
            ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
            ->where("o.user_id=$user_id AND (c.type=0 OR c.type=1)")
            ->orderBy('c.cancel_time desc')
            ->asArray()
            ->all();
    }

    public function getIncomeByShop($shop_id,$start_time,$end_time){
        return Income::find()
            ->select('c.*,c.type as type,sum(i.commission) as total_commission,i.cancel_id as cancel_id')
            ->from('{{%income}} i')
            ->leftJoin('{{%cancel}} c','c.cancel_id=i.cancel_id')
            ->where("c.shop_id=$shop_id AND cancel_time>='$start_time' AND cancel_time<='$end_time'")
            ->groupBy('i.cancel_id')
            ->asArray()
            ->all();
    }

    public function getCancelListByShop($shop_id,$start_time,$end_time,$type){
        if($type){
            return Cancel::find()
                ->select('c.*,o.*,s.*')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
                ->where("c.shop_id=$shop_id AND cancel_time>=$start_time AND cancel_time<=$end_time")
                ->orderBy('o.order_id desc')
                ->asArray()
                ->all();
        }else{
            return Cancel::find()
                ->select('c.*,o.*,s.*')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
                ->where("c.shop_id=$shop_id")
                ->orderBy('o.order_id desc')
                ->asArray()
                ->all();
        }

    }



    public function getCancelListByMyself($user_id,$shop_ids,$start_time,$end_time,$type){
        if($type==1){
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,c.type as cancel_type, o.type as order_type,o.price as price')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->where(['c.shop_id'=>$shop_ids])
                ->andWhere("'$start_time'<=c.cancel_time AND '$end_time'>=c.cancel_time")
                ->asArray()
                ->all();
        }else{
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,c.type as cancel_type, o.type as order_type,o.price as price')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                ->where("m.user_id=$user_id")
                ->andWhere("'$start_time'<=c.cancel_time AND '$end_time'>=c.cancel_time")
                ->asArray()
                ->all();
        }

    }

    public function getCancelListAndIncomeByMyself($user_id,$shop_id,$start_time,$end_time,$type){
        if($type==1){
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,d.*,c.type as cancel_type, o.type as order_type,o.price as price,i.*,p.type as ptype')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%device}} d','d.device_id=o.gp_id  AND o.type=2')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%income}} i','i.cancel_id=c.cancel_id and i.type=1')
                ->where(['c.shop_id'=>$shop_id])
                ->andWhere("'$start_time'<=c.cancel_time AND '$end_time'>=c.cancel_time")
                ->orderBy('c.cancel_id desc')
                ->asArray()
                ->all();
        }else{
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,d.*,c.type as cancel_type, o.type as order_type,o.price as price,i.*,p.type as ptype')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%device}} d','d.device_id=o.gp_id  AND o.type=2')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                ->leftJoin('{{%income}} i','i.cancel_id=c.cancel_id and i.type=2')
                ->where("m.user_id=$user_id")
                ->andWhere("'$start_time'<=c.cancel_time AND '$end_time'>=c.cancel_time")
                ->orderBy('c.cancel_id desc')
                ->asArray()
                ->all();
        }

    }

    public function getCommissionConfig(){
        return Config::find()->where(['config_name'=>'commission'])->asArray()->all();
    }

    public function findOrderByCancelNo($cancel_no){
        return Order::findOne(['cancel_no'=>$cancel_no]);
    }

    public function getCanUseCard($user_id){
        $ordertemp = Order::find()
            ->from('{{%order}} o')
            ->leftJoin('{{%play}} p','p.play_id=o.gp_id')
            ->where("o.user_id=$user_id AND o.type=0 AND o.current_num>0 AND p.type=1")
            ->orderBy('o.order_id')
            ->asArray()
            ->all();
        if($ordertemp){
            foreach ($ordertemp as $key=>$value){
                if($value['status']==3)
                    return 3;
            }
            return 1;
        }else{
            return false;
        }

    }

}
